package com.example.androidhw06_tab;

import java.util.ArrayList;
import java.util.List;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.packet.Message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.example.androidhw06_tab.MoreTab.LoginInfo;
import com.example.androidhw06_tab.XMPPManager.OnLoginListener;
import com.example.androidhw06_tab.XMPPManager.OnMessageReceiveListener;
import com.example.androidhw06_tab.XMPPManager.OnRosterListener;

import friend.FriendAdapter;

public class FriendTab extends Fragment{
	
	ListView list;
	ArrayAdapter<MyFriend> mAdapter;

	ArrayList<MyFriend> mList = new ArrayList<MyFriend>();
		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.fragment_friend, container, false);
		list = (ListView)v.findViewById(R.id.friendlist);
		
		if(LoginInfo.getInstance() != null){
			XMPPManager.getInstance().login(LoginInfo.getInstance()._id, LoginInfo.getInstance()._pw, new OnLoginListener() {
				
				@Override
				public void onLoginSuccess(String username) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "login success", Toast.LENGTH_SHORT).show();
					XMPPManager.getInstance().getRoster(new OnRosterListener() {
						

						@Override
						public void onRoasterReceived(List<MyFriend> users) {
							// TODO Auto-generated method stub
							//mAdapter = new FriendAdapter(getActivity(), mList);
							mAdapter = new ArrayAdapter<MyFriend>(getActivity(),android.R.layout.simple_list_item_1,users);
							list.setAdapter(mAdapter);
						}
					});
				}
				
				@Override
				public void onLoginFail(String username) {
					// TODO Auto-generated method stub
					Toast.makeText(getActivity(), "login failed", Toast.LENGTH_SHORT).show();
				}
			});
		}
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				MyFriend user = mAdapter.getItem(position);
				Intent i = new Intent(getActivity(), MessageActivity.class);
				i.putExtra(MessageActivity.PARAM_USER, user.user.getUser());
				startActivity(i);
			}
		});
		XMPPManager.getInstance().addOnMessageReceiveListener(new OnMessageReceiveListener() {
			
			@Override
			public void onMessageReceived(Chat chat, Message message) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "message : " + message.getBody(), Toast.LENGTH_SHORT).show();
			}
		});
//		mAdapter = new FriendAdapter(getActivity(), mList);
//		list.setAdapter(mAdapter);
		return v;
	}
}
